﻿//==============================================================================
// File: ApplicationOptionsViewPresentationModel.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using WpfRcp.Core.Services;
using WpfRcp.Core.Views;
using WpfRcp.Core.Views.Presenters;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.ServiceLocation;

namespace WpfRcp.Views.Presenters
{
    public class ApplicationOptionsViewPresentationModel : PresenterBase<IApplicationOptionsView>
    {
        private readonly IApplicationSettings m_Settings;
        private readonly IServiceLocator m_ServiceLocator;
        private readonly IRcpSettings m_RcpSettings;

        public DelegateCommand<object> CloseCommand { get; private set; }
        public DelegateCommand<object> RestoreDefaultsCommand { get; private set; }

        public ApplicationOptionsViewPresentationModel(
            IApplicationOptionsView view,
            IShell shell,
            IRcpSettings rcpSettings,
            IApplicationSettings settings,
            IServiceLocator serviceLocator)
        {
            m_Settings = settings;
            m_ServiceLocator = serviceLocator;
            m_RcpSettings = rcpSettings;

            CloseCommand = new DelegateCommand<object>(ExecuteCloseCommand);
            RestoreDefaultsCommand = new DelegateCommand<object>(ExecuteRestoreDefaultsCommand);

            View = view;
            View.PresentationModel = this;
            View.ViewOwner = shell;
        }

        public IRcpSettings RcpSettings
        {
            get { return m_RcpSettings; }
        }

        private void ExecuteCloseCommand(object arg)
        {
            View.Close();
        }

        private void ExecuteRestoreDefaultsCommand(object arg)
        {
            var messageBox = m_ServiceLocator.GetInstance<IMessageBox>();
            var result = messageBox.Show(Properties.Resources.Question, Properties.Resources.RestoreDefaultSettings,
                                         MessageBoxViewButtons.YesNo, MessageBoxViewIcons.Question);

            if (result == MessageBoxViewResult.Yes)
            {
                m_Settings.RestoreDefaults();
            }
        }
    }
}